package ess.control;

import java.util.Collections;
import java.util.Iterator;
import java.util.Vector;

import ess.hardware.FloorOperationEvent;
import ess.hardware.FloorOperationEventListener;
import ess.hardware.ElevatorSimulationFrame;
import ess.hardware.Direction;
import ess.hardware.ElevatorBuilder;
import ess.control.ElevatorStates;

public class ElevatorManager implements FloorOperationEventListener{
	static ElevatorManager instance = null;
	Vector elevatorProxies;
		 
	static ElevatorSimulationFrame elevatorSimulator;	
	public static ElevatorManager getInstance() {
		if (instance == null) {
			// singleton instance
			instance = new ElevatorManager();
		}
		return instance;
	}

	private ElevatorManager() {
		elevatorProxies = new Vector();
	}

	public void operationOccured(FloorOperationEvent event) {
		ElevatorStructure elvstruct = new ElevatorStructure();
		Vector floortemplist = new Vector();	
			//System.out.println(event.getOperationFloor());
			//System.out.println(event.getOperationValue());
			//System.out.println(event.VALUE_DOWN);
			//System.out.println(event.VALUE_UP);
			//System.out.println(event.hashCode());
						
			if(FloorOperationEvent.VALUE_DOWN == event.getOperationValue())
			{
				if(event.getOperationFloor() != 1)
				{
					String strDownFloor = String.valueOf(event.getOperationFloor()); 
					String strDownvalue = String.valueOf(event.getOperationValue());
				
			//		floortemplist.addElement(strDownFloor);
			//		floortemplist.addElement(strDownvalue);
				
					elvstruct.updownFloor = event.getOperationFloor();
					elvstruct.updownValue = event.getOperationValue();				
					floortemplist.addElement(elvstruct);
				}
				else
				{
					return;
				}				
			}else{
				
				if(event.getOperationFloor() != 7)
				{
					String strUpFloor = String.valueOf(event.getOperationFloor());
					String strUpvalue = String.valueOf(event.getOperationValue());
					
					elvstruct.updownFloor = event.getOperationFloor();
					elvstruct.updownValue = event.getOperationValue();				
					floortemplist.addElement(elvstruct);
					
				}else
				{
					return;
				}
			}
			
			//System.out.println(floortemplist);
			ElevatorScheduler elesch = new ElevatorScheduler();
			elesch.setElevator1(elevatorSimulator.getElevator(1));
 		    elesch.setElevator2(elevatorSimulator.getElevator(2));
			elesch.setFloorCall(floortemplist);
			
	}

	public void notifyElevatorArrived(ElevatorProxy proxy, int floor) {

	}

	public void registerElevator(ess.hardware.Elevator elevator) {
		ElevatorProxy proxy = new ElevatorProxy(elevator);
		//elevatorProxies.add(proxy);
		elevator.addArrivalSensorEventListener(proxy);
		elevator.addDoorClosingSensorEventListener(proxy);
		elevator.addObtacleEventListener(proxy);
		elevator.addElevatorOperationEventListener(proxy);
		
	}

	public static void main(String[] args) {
		int numOfElevator = 2;
		ElevatorBuilder.setNumOfElevator(numOfElevator);
		elevatorSimulator = ElevatorBuilder.getElevatorSimulationFrame();
		for (int i = 0; i < numOfElevator; i++){
			getInstance().registerElevator(elevatorSimulator.getElevator(i + 1));
		}
		elevatorSimulator.addFloorOperationEventListener(getInstance());
		elevatorSimulator.startSimulation();
	}
}
